package org.west.sky.frame.activiti.study.chapter14.demo0104;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateHelper;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.impl.delegate.ActivityBehavior;

/**
 * author: chz
 * date: 2025/3/21
 * description:
 */
public class InventoryCheckBehavior implements ActivityBehavior {


    private int inventoryNum = 5;

    @Override
    public void execute(DelegateExecution execution) {
        System.out.println("====开始计算");
        Object inventory = execution.getVariable("inventory");
        Object description = execution.getVariable("description");
        System.out.println("====描述：" + description);

        //物资发放
        String nextSequenceFlow = "sid-6b3a9fc8-7063-424e-a36a-8ab20702abe5";
        try {
            check(Integer.parseInt(inventory.toString()));
        } catch (Exception e) {
            System.out.println("====抛出异常：" + e.getMessage());
            nextSequenceFlow = "sid-8915b884-6bc3-434e-9267-95d242caa2dd";
        }
        DelegateHelper.leaveDelegate(execution, nextSequenceFlow);
    }

    private void check(int inventory) {
        if (inventory > inventoryNum) {
            throw new RuntimeException("库存不足");
        }
    }
}
